<?php

/**
 * This is the model class for table "orders".
 *
 * The followings are the available columns in table 'orders':
 * @property integer $id
 * @property string $account_id
 * @property string $customer_name
 * @property string $customer_phone
 * @property string $billing_address
 * @property string $address
 * @property string $order_date
 * @property string $note
 * @property integer $status
 * @property string $deadline
 * @property string $ngay_giao_hang
 * @property integer $hinh_thuc_thanh_toan
 * @property integer $tinhid
 * @property integer $hinh_thuc_van_chuyen
 * @property double $total
 *
 * The followings are the available model relations:
 * @property NoteOrder[] $noteOrders
 * @property OrderThanhtoan[] $orderThanhtoans
 * @property Orderdetails[] $orderdetails
 * @property Account $account
 * @property HinhThucThanhToan $hinhThucThanhToan
 * @property HinhThucVanChuyen $hinhThucVanChuyen
 * @property StatusOrder $status0
 * @property Tinh $tinh
 * @property PhiVanChuyen $phiVanChuyen
 */
class Orders extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'orders';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('account_id', 'required'),
			array('status, hinh_thuc_thanh_toan, tinhid, hinh_thuc_van_chuyen', 'numerical', 'integerOnly'=>true),
			array('total', 'numerical'),
			array('account_id, billing_address, address, note', 'length', 'max'=>255),
			array('customer_name', 'length', 'max'=>100),
			array('customer_phone', 'length', 'max'=>20),
			array('order_date, deadline, ngay_giao_hang', 'safe'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, account_id, customer_name, customer_phone, billing_address, address, order_date, note, status, deadline, ngay_giao_hang, hinh_thuc_thanh_toan, tinhid, hinh_thuc_van_chuyen, total', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'noteOrders' => array(self::HAS_MANY, 'NoteOrder', 'order_id'),
			'orderThanhtoans' => array(self::HAS_MANY, 'OrderThanhtoan', 'orderid'),
			'orderdetails' => array(self::HAS_MANY, 'Orderdetails', 'order_id'),
			'account' => array(self::BELONGS_TO, 'Account', 'account_id'),
			'hinhThucThanhToan' => array(self::BELONGS_TO, 'HinhThucThanhToan', 'hinh_thuc_thanh_toan'),
			'hinhThucVanChuyen' => array(self::BELONGS_TO, 'HinhThucVanChuyen', 'hinh_thuc_van_chuyen'),
			'status0' => array(self::BELONGS_TO, 'StatusOrder', 'status'),
			'tinh' => array(self::BELONGS_TO, 'Tinh', 'tinhid'),
			'phiVanChuyen' => array(self::HAS_ONE, 'PhiVanChuyen', 'id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'account_id' => 'Account',
			'customer_name' => 'Customer Name',
			'customer_phone' => 'Customer Phone',
			'billing_address' => 'Billing Address',
			'address' => 'Address',
			'order_date' => 'Order Date',
			'note' => 'Note',
			'status' => 'Status',
			'deadline' => 'Deadline',
			'ngay_giao_hang' => 'Ngay Giao Hang',
			'hinh_thuc_thanh_toan' => 'Hinh Thuc Thanh Toan',
			'tinhid' => 'Tinhid',
			'hinh_thuc_van_chuyen' => 'Hinh Thuc Van Chuyen',
			'total' => 'Total',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('account_id',$this->account_id,true);
		$criteria->compare('customer_name',$this->customer_name,true);
		$criteria->compare('customer_phone',$this->customer_phone,true);
		$criteria->compare('billing_address',$this->billing_address,true);
		$criteria->compare('address',$this->address,true);
		$criteria->compare('order_date',$this->order_date,true);
		$criteria->compare('note',$this->note,true);
		$criteria->compare('status',$this->status);
		$criteria->compare('deadline',$this->deadline,true);
		$criteria->compare('ngay_giao_hang',$this->ngay_giao_hang,true);
		$criteria->compare('hinh_thuc_thanh_toan',$this->hinh_thuc_thanh_toan);
		$criteria->compare('tinhid',$this->tinhid);
		$criteria->compare('hinh_thuc_van_chuyen',$this->hinh_thuc_van_chuyen);
		$criteria->compare('total',$this->total);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Orders the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
}
